﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vara_Data_Layer.Vara_Data_Asset;
using Vara_Data_Layer.Vara_Data_Audit_Schedule;
using Vara_Data_Layer.Vara_Data_Audit_History;

namespace AssetTrackingModals.User_Controls.Asset_Module.Tabs
{
    public partial class Tab_5_Maintenance : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public void Load_Interface(Vara_Data_IAsset AssetInformationToLoad)
        {
            //Set the hidden asset ID to pass to the audit modal
            HiddenAssetId.Value = AssetInformationToLoad.ID.ToString();

            //Populate the drop down list of audit schedules
            PopulateDropDownListAuditSchedules(AssetInformationToLoad);

            //If an audit schedule has been entered, then select it as the default on the drop down list
            SetAuditSchedule(AssetInformationToLoad);

            //Set the initial audit date
            SetInitialAuditDate(AssetInformationToLoad);

            //Set the gridview of audit histories
            GridViewAuditHistories.DataSource = new Vara_Data_Audit_History_Controller().AllAuditsForAsset(AssetInformationToLoad.ID);
            GridViewAuditHistories.DataBind();
        }
        protected void PopulateDropDownListAuditSchedules(Vara_Data_IAsset AssetInformationToLoad)
        {
            DropDownList_Audit_Schedule.DataSource = new Vara_data_Audit_Schedule_Controller().AuditSchedules;
            DropDownList_Audit_Schedule.DataTextField = "Name";
            DropDownList_Audit_Schedule.DataValueField = "ID";
            DropDownList_Audit_Schedule.DataBind();
        }
        protected void SetInitialAuditDate(Vara_Data_IAsset AssetInformationToLoad)
        {
            //If there is an initial audit date that isn't equal to the default setting (null) or null, then set it
            DateTime DefaultDateTime = DateTime.Parse("01/01/0001 00:00:00");
            if (AssetInformationToLoad.InitialAuditDate != null && AssetInformationToLoad.InitialAuditDate != DefaultDateTime)
            {
                CalendarTextBox_Initial_Audit_date.SelectedDate = AssetInformationToLoad.InitialAuditDate;
            }
            else
            {
                CalendarTextBox_Initial_Audit_date.SelectedDate = DateTime.Now.AddYears(1);
            }
        }
        //if there is an audit schedule then set it
        protected void SetAuditSchedule(Vara_Data_IAsset AssetInformationToLoad)
        {
            if (AssetInformationToLoad.AuditSchedule != null)
            {
                if (DropDownList_Audit_Schedule.Items.FindByValue(AssetInformationToLoad.AuditSchedule.ID.ToString()) != null)
                {
                    DropDownList_Audit_Schedule.Items.FindByValue(AssetInformationToLoad.AuditSchedule.ID.ToString()).Selected = true;
                }
            }
            else
            {
                if (DropDownList_Audit_Schedule.Items.Contains(DropDownList_Audit_Schedule.Items.FindByText("1 Year")))
                {
                    DropDownList_Audit_Schedule.Items.FindByText("1 Year").Selected = true;
                }
            }
        }
        protected void ButtonAddAudit_Click(object sender, EventArgs e)
        {
            Modal_Audit1.Show(int.Parse(HiddenAssetId.Value.ToString()), 0);
        }
        protected void GridViewAuditHistoryCommandControls(object sender, EventArgs e)
        {
            GridViewRow GridViewRowCurrentRow = ((GridViewRow)((ImageButton)sender).NamingContainer);
            ImageButton ImageButtonSent = ((ImageButton)sender);

            if (ImageButtonSent.CommandName == "EditAudit")
            {
                Modal_Audit1.Show(int.Parse(HiddenAssetId.Value.ToString()), int.Parse(GridViewAuditHistories.DataKeys[GridViewRowCurrentRow.RowIndex].Values[0].ToString()));
                RefreshAuditHistory();
            }
            else if (ImageButtonSent.CommandName == "DeleteAudit")
            {
                Vara_Data_Audit_History_Controller VaraDataAuditHistoryController = new Vara_Data_Audit_History_Controller();
                VaraDataAuditHistoryController.DeleteAuditHistory(int.Parse(GridViewAuditHistories.DataKeys[GridViewRowCurrentRow.RowIndex].Values[0].ToString()));
                RefreshAuditHistory();
            }
        }
        protected void RefreshAuditHistory()
        {
            GridViewAuditHistories.DataSource = new Vara_Data_Audit_History_Controller().AllAuditsForAsset(int.Parse(HiddenAssetId.Value.ToString()));
            GridViewAuditHistories.DataBind();
        }
    }
}